package com.service;

import com.domain.*;
import com.github.pagehelper.PageHelper;
import com.mapper.BlogArticleMapper;
import com.mapper.CommentMapper;
import com.mapper.StatisticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service

    public class SiteService {
        @Autowired
        private CommentMapper commentMapper;
        @Autowired
        private BlogArticleMapper articleMapper;
        @Autowired
        private StatisticMapper statisticMapper;

        public void updateStatistic(BlogArticle article){
            Statistic statistic = statisticMapper.selectStatisticArticleWithId(article.getId());
            statistic.setHits(statistic.getHits()+1);
            statisticMapper.updateArticleHitsWithId(statistic);
        }
        public List<BlogComment> recentComments (int limit) {
            PageHelper.startPage(1,limit>10 || limit<1 ? 10:limit);
            List<BlogComment> byPage = commentMapper.selectNewComment();
            return byPage;
        }
        public List<BlogArticle> recentArticles(int limit) {
            PageHelper.startPage(1,limit>10 || limit<1 ? 10:limit);
            List<BlogArticle> list = articleMapper.selectArticleWithPage();
            for (int i = 0; i < list.size();i++){
                BlogArticle article = list.get(i);
                Statistic statistic = statisticMapper.selectStatisticArticleWithId(article.getId());
                article.setHits(statistic.getHits());
                article.setCommentsNum(statistic.getCommentsNum());
            }
            return list;
        }
        public StaticticBo getStatistics(){
            StaticticBo statisticBo = new StaticticBo();
            Integer articles = articleMapper.countArticle();
            Integer comments = commentMapper.countComment();
            statisticBo.setArticles(articles);
            statisticBo.setComments(comments);
            return statisticBo;
        }
    }

